<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>31_节点操作</title>
    <link rel="stylesheet" href="./css/basic.css" />
    <style>
      .container {
        width: 100vw;
        height: 100vh;
        background-color: pink;
        padding: 10px;
        box-sizing: border-box;
      }
      .content {
        width: 100%;
        height: 75%;
        background-color: #fff;
        padding: 20px;
        box-sizing: border-box;
      }
      .input {
        height: 20%;
        width: 100%;
        position: relative;
        margin-top: 20px;
      }
      .input textarea,
      .input button {
        position: absolute;
        resize: none;
      }
      .input textarea {
        width: 100%;
        height: 100%;
        padding: 10px;
        box-sizing: border-box;
      }
      .input button {
        right: 10px;
        bottom: 10px;
        width: 100px;
        height: 50px;
        background-color: blue;
        color: #fff;
        font-size: 20px;
      }
      .message {
        color: brown;
        font-weight: bolder;
        font-size: 30px;
        margin-right: 20px;
      }
      .datetime {
        color: #000;
        font-size: 12px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="content"></div>
      <!--  聊天区 -->

      <div class="input">
        <!-- 输入框 -->
        <textarea name="" id="contentArea"></textarea>
        <button id="btnSend"></button>
      </div>
    </div>

    <script>
      const btnDom = document.querySelector("#btnSend"); // 发送按钮对象
      const textareaDom = document.querySelector("#contentArea"); // 输入区对象
      const contentDom = document.querySelector(".content"); // 聊天区对象
      enableBtn();
      textareaDom.addEventListener("input", (event) => {
        enableBtn(); // 控制按钮是否可以点击
      });
      btnDom.addEventListener("click", (event) => {
        sendMsg();
      });

      textareaDom.addEventListener("keydown", (event) => {
        if (event.key === "Enter") {
          sendMsg();
        }
      });

      function enableBtn() {
        if (textareaDom.value) {
          // 删除diabled 属性
          btnDom.removeAttribute("disabled");
        } else {
          btnDom.setAttribute("disabled", true);
        }
      }
      // 发送消息
      function sendMsg() {
        const content = textareaDom.value;
        const p = document.createElement("p");
        p.innerHTML = `<span class= "message">${content}</span><span class="datetime">${new Date().toLocaleString()}</span>`;
        contentDom.append(p);
        textareaDom.value = "";
      }
    </script>
  </body>
</html>
